package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserService;
import util.MessageUtil;
import util.ReturnResultUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by gable on 2018/6/20.
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Resource
    HttpServletRequest request;


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public ReturnResultUtil login(String username,String password){
        try {
            ReturnResultUtil rru = userService.login(username,password);
            if(rru.getRet() == 0){
                return rru;
            }else {
                request.getSession().setAttribute("loginUser", rru.getData());
                return rru;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0, MessageUtil.ERROR);
        }

    }

    @RequestMapping(value = "/login-error",method = RequestMethod.GET)
    @ResponseBody
    public ReturnResultUtil login(){
        try {
            return new ReturnResultUtil(1,"请登录");
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0, MessageUtil.ERROR);
        }
    }


}
